package xin.yangshuai.springsecurity03.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import xin.yangshuai.common01.entity.BaseResult;
import xin.yangshuai.springsecurity03.entity.SpringSecurityUser;
import xin.yangshuai.springsecurity03.service.SpringSecurityUserService;

import java.util.List;

@RestController
@RequestMapping("user")
public class SpringSecurityUserController {

    @Autowired
    private SpringSecurityUserService springSecurityUserService;

    @GetMapping("list")
    @PreAuthorize("hasAuthority('USER_LIST')")
    public BaseResult<List<SpringSecurityUser>> list() {

        List<SpringSecurityUser> list = springSecurityUserService.list();

        BaseResult<List<SpringSecurityUser>> result = new BaseResult<>();
        result.setCode("200");
        result.setData(list);
        return result;
    }

    @PostMapping("add")
    public BaseResult<Integer> add(@RequestBody SpringSecurityUser springSecurityUser) {

        int rows = springSecurityUserService.add(springSecurityUser);

        BaseResult<Integer> result = new BaseResult<>();
        result.setCode("200");
        result.setData(rows);
        return result;
    }
}
